package com.hamster.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crm.exception.ExceptionEnum;
import com.hamster.crm.exception.LyException;
import com.hamster.crm.mapper.OpinionaireMapper;
import com.hamster.crm.pojo.Opinionaire;
import com.hamster.crm.service.OpinionaireService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 问卷调查的  Service
 * @author Tarry
 * @create 2019/12/23 11:22
 */
@Service
public class OpinionaireServiceImpl implements OpinionaireService {
    @Autowired
    private OpinionaireMapper opinionaireMapper;
    /**
     * 新增问卷
     * @param opinionaire
     */
    @Override
    public void insert(Opinionaire opinionaire) {

        if (StringUtils.isBlank(opinionaire.getTitle())){
            throw new LyException(ExceptionEnum.TITLE_IS_NOT_NULL);
        }

        Date date = new Date();
        opinionaire.setCreateTime(date);
        opinionaire.setInputTime(date);
        opinionaire.setInputUser("功能待完善");
        opinionaire.setDel("0");
        opinionaire.setState("0");   //初始状态为  0新建
        opinionaireMapper.insert(opinionaire);
    }

    /**
     * 根据id删除问卷
     * @param ids id集合
     */
    @Transactional
    @Override
    public void delete(List<String> ids) {
        for (String id : ids) {
            opinionaireMapper.deleteTag(id);  //逻辑删除，修改del = 1
        }

    }

    /**
     * 后台管理根据id，下发问卷
     * @param ids id集合
     */
    @Transactional
    @Override
    public void send(List<String> ids) {
        for (String id : ids) {
            //修改问卷状态为  1 已下发
            opinionaireMapper.updateOpinionaireStateById(id,"1");
        }
    }

    /**
     * 根据id，获取问卷
     * @param id 问卷id
     * @return
     */
    @Override
    public Opinionaire get(String id) {
        return opinionaireMapper.selectById(id);
    }

    /**
     * 问卷列表
     * @return List<Opinionaire>
     */
    @Override
    public List<Opinionaire> list() {
//        QueryWrapper<Opinionaire> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("del","0");
//        queryWrapper.orderByDesc("create_time");  //按时间降序
//        return opinionaireMapper.selectList(queryWrapper);

        return opinionaireMapper.selectOpinionaireList();
    }
}
